home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / wrap_it.zip / WRAP_IT.PAS < prev   
Pascal/Delphi Source File  |  1990-04-27  |  3KB  |  91 lines

  1. PROGRAM Wrap_it;
  2.  
  3. {* -------------------------------------------------------------- *}
  4. {*  WRAP_IT.PAS - a routine to do wordwrap in text files.         *}
  5. {*                                                                *}
  6. {*  Written by LOU GARNER, and released into the public domain.   *}
  7. {*                                                                *}
  8. {* -------------------------------------------------------------- *}
  9.  
  10. USES
  11.   Crt;    {uses the ReadKey function}
  12.  
  13. CONST
  14.   MaxChars = 60;    {this is right hand margin}
  15.  
  16. VAR
  17.   OneLine, Buffer : String[80];
  18.   CharCount : Word;
  19.   OneChar : Char;
  20.  
  21. BEGIN    {MAIN}
  22.   ClrScr;
  23.   OneLine := '';    {revise to put all in an init module}
  24.   CharCount := 0;
  25.   OneChar := ' ';
  26.   WHILE (OneChar <> #027) DO
  27.     BEGIN
  28.       REPEAT        {one loop per character typed}
  29.         BEGIN
  30.           OneChar := ReadKey;  {get the char W/O echo to the CRT}
  31.           CASE OneChar of
  32.             #08   :  BEGIN                  {backspace}
  33.                        Write(#08,#032,#08); {wipe it off the screen}
  34.                        IF (Length(OneLine) > 0) THEN
  35.                          Delete(OneLine,CharCount,1);
  36.                              {error trap use of BS on Column One}
  37.                        CharCount := Length(OneLine);  {equal to good chars}
  38.                      END;
  39.             #013  :  BEGIN
  40.                        Writeln;          {issue a CR to the screen}
  41.                        CharCount := 0;   {re-init the index}
  42.                        OneLine := '';    {re-init the output line}
  43.                      END;
  44.             #027  :  Exit   {yeah, I know, sloppy exit....}
  45.             ELSE
  46.               BEGIN     {main routine to accept text characters}
  47.                 OneLine := OneLine + OneChar;   {add to the output line}
  48.                 Write(OneChar);                 {& put it on the screen}
  49.                 Inc(CharCount);                 {bump the index}
  50.               END;
  51.             END;
  52.           END;
  53.   UNTIL (CharCount = MaxChars);
  54.          {go to MaxChars before wrapping}
  55.  
  56.  {word wrapping routine now takes over ... }
  57.  
  58.      IF (OneLine[CharCount] = #032) THEN   {If end of line is a #032}
  59.        BEGIN
  60.          Writeln;       {issue a CR to the screen}
  61.          CharCount := 0;   {re-init the index}
  62.          OneLine := '';    {re-init the output line}
  63.              {and you're at the start of the new line ... }
  64.        END
  65.      ELSE     {if end of line is <> a space }
  66.        BEGIN
  67.          Buffer := '';   {init the buffer for bopped characters }
  68.          WHILE (OneLine[CharCount] <> #032) DO
  69.                    {until we find a space at which to wrap}
  70.            BEGIN
  71.              Buffer := OneLine[CharCount] + Buffer;
  72.                    {add the character to the FRONT of the buffer}
  73.              Dec(CharCount);    {bump the index DOWN}
  74.              Write(#08,#032,#08);  {wipe out the char on the screen}
  75.            END;
  76.  
  77.      {by this point we've found a #032, so ... }
  78.  
  79.          CharCount := 0;             {re-init the index}
  80.          Writeln;                    {issue a CR to the screen}
  81.          Write(Buffer);              {write the buffer to new line
  82.                                          on the screen}
  83.          OneLine := Buffer;          {and catch up the output line}
  84.          Buffer := '';               {re-init the buffer}
  85.      END;
  86.   END;
  87. Writeln;    {Clean up the screen, if necessary}
  88. END.
  89.  
  90.  
  91.